﻿using Ioc.Unity;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;

namespace BackstageWeb.App_Start.Helper
{
    public class BaseApiController : ApiController
    {
        /// <summary>
        /// HTTP上下文
        /// </summary>
        protected HttpContextBase Context { get; set; }

        /// <summary>
        /// 从请求传递过来的数据
        /// </summary>
        protected Dictionary<string, object> RequestFormParameter { get; set; }

        /// <summary>
        /// 进入Action前封装请求的数据
        /// </summary>
        /// <param name="requestContext">请求上下文</param>
        /// <exception cref="Exception"></exception>
        protected override void Initialize(HttpControllerContext requestContext)
        {

            // 获取传统context
            this.Context = (HttpContextBase)requestContext.Request.Properties["MS_HttpContext"];

            base.Initialize(requestContext);
            if (Context.Request.RequestType == "POST")
            {
                try
                {
                    // 解析数据入口
                    //if (!string.IsNullOrWhiteSpace(Context.Request.InputStream.ToString()))
                    //{
                    //	//解析数据入口
                    //	if (this.Context.Request.Url.AbsoluteUri.ToLower().Contains("/payment/notify"))
                    //	{
                    //		this.RequestFormParameter = new Dictionary<string, object>();

                    //		foreach (var item in Context.Request.Form.Keys)
                    //		{
                    //			RequestFormParameter.Add(item.ToString(), Context.Request.Form[item.ToString()]);
                    //		}
                    //	}
                    //	else
                    //	{
                    //		this.RequestFormParameter = JsonConvert.DeserializeObject<Dictionary<string, object>>(HttpUtility.UrlDecode(new StreamReader(Context.Request.InputStream).ReadToEnd()));
                    //	}
                    //}

                    if (!string.IsNullOrWhiteSpace(Context.Request.Form.ToString()))
                    {
                        this.RequestFormParameter = JsonConvert.DeserializeObject<Dictionary<string, object>>(HttpUtility.UrlDecode(Context.Request.Form.ToString()));
                    }

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }



        /// <summary>
        /// 获取注入对象
        /// </summary>
        /// <typeparam name="T">接口类型</typeparam>
        /// <returns>实现接口的实例</returns>
        protected T GetInjectionInstance<T>()
        {
            return UnityCIContainer.Instance.GetService<T>();
        }

        /// <summary>
        /// 返回JSON
        /// </summary>
        /// <param name="data"></param>
        /// <returns>处理结果</returns>
        protected static string ResultJson(object data)
        {
            return JsonConvert.SerializeObject(data, new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });
        }
    }
}